package basicprocess;

import java.util.Hashtable;
import property.Property;
import pool.tablename.Pool;
import utility.CapitalChar;
import utility.Input;
import database.RecordSearch;
import dbmanager.Column;
import dbmanager.DBManager;
import dbmanager.GetIdName;


public class Authentication implements Basicprocessinterface  
{
	public Pool pool;
	public DBManager database;
	String USER="user";
	String PERSONCODE="personcode";
	String USERID="userid";
	GetIdName gid;
	RecordSearch sr;
	String AUTHENTICATION = "authentication"; 
	String MASTER="master";
	String GROUP="group";
	Hashtable<Object, Object> Result = new Hashtable<Object, Object>();
	String AUTENTICATIONERROR="ErrorInput";
	Hashtable<Object, Object> Error =new Hashtable<Object, Object>();
	
	public void getInsert() 
	{
		
	}

	public void getUpdate() 
	{
		
	}

	public void processBasicprocess() 
	{
/*
		String type=pool.getType();
		try 
		{
			String Userid=  pool.getRequestParameter().getUsername();
			Userid = gid.getId(Userid);
			Hashtable<Object, Object> table =new Hashtable<Object, Object>();
			table.put("mid", Userid);
			table.put("pid",AUTHENTICATION);
			sr.setConditionMap(table);
			String line[] = sr.getArrayFromResultSet();	
			table.clear();
			table.put("mid", Userid);
			table.put("td", "null");
			table.put("pid", GROUP);
			sr.setConditionMap(table);
			String temp[] = sr.getArrayFromResultSet();
			String result[]={};
			for(int i=0;i<temp.length;i++)
			{
				String splitline[]=temp[i].split("\t");
				table.clear();
				table.put("mid", splitline[Column.pv_index-1]);
				table.put("pid", AUTHENTICATION);
				table.put("td", "null");
				sr.setConditionMap(table);
				result = sr.getArrayFromResultSet();
			}
			
			temp  = new String[result.length+line.length];
			for(int i=0;i<result.length;i++)
			{
				temp[i]=result[i];
			}
			for(int i=0;i<line.length;i++)
			{
				temp[i]=line[i];
			}
			
			line = temp;
			String splitline[] = line[0].split("\t");
			
			table.clear();
			table.put("mid",splitline[Column.pv_index-1]);
			table.put("pid", type);
			table.put("td", "null");
			sr.setConditionMap(table);
			line = sr.getArrayFromResultSet();
			if(line.length==0)
				Error.put(AUTENTICATIONERROR, "You are not authorized to  "+pool.getRequestParameter().getProcess()+" "+gid.getItem(type));
			for(int i=0;i<line.length;i++)
			{
				splitline = line[i].split("\t");
				basicprocess.authentication.Authentication authenticationonject;
				try 
				{	
					Property gp =new Property(database);
					gp.setMid(splitline[Column.pv_index-1]);
					gp.setPid(gid.getId("type"));
					String classname= gp.getValue();	
					Class c = Class.forName("basicprocess.authentication."+CapitalChar.makeFirstCharCapital(classname));
					authenticationonject = (basicprocess.authentication.Authentication) c.newInstance();
					authenticationonject.setobject(this, splitline[Column.pv_index-1]);
					if(authenticationonject.getResult())
						Error.put(AUTENTICATIONERROR, "You are not authorized to  "+pool.getRequestParameter().getProcess()+" "+gid.getItem(type));
					else
					{
						Error.clear();
						break;
					}	
				}
				catch (ClassNotFoundException e) 
				{
					e.printStackTrace();
				} 
				catch (InstantiationException e) 
				{
					e.printStackTrace();
				}
				catch (IllegalAccessException e) 
				{
					e.printStackTrace();
				}
			}
		} catch (Exception e) 
		{	
			e.printStackTrace();
			Error.put(AUTENTICATIONERROR, "You are not authorized to  "+pool.getRequestParameter().getProcess()+" "+gid.getItem(type));
		}*/
	}

	public void setDbmanager(DBManager database) 
	{
		this.database = database;
		gid =new GetIdName(database);
		AUTHENTICATION = gid.getId(AUTHENTICATION);
		MASTER =  gid.getId(MASTER);
		USERID = gid.getId(USERID);
		PERSONCODE = gid.getId(PERSONCODE);
		GROUP= gid.getId(GROUP);
		sr =new RecordSearch(database); 
	}

	public void setObject(Object obj) 
	{
		pool = (Pool) obj;
	}
	public Hashtable<Object, Object> getHashtable()
	{
		return Error;
	}
	public boolean checkacess(String rights)
	{
		String Request = pool.getRequestParameter().getProcess();
		String requestfirstchar = Request.substring(0, 1);
		if(rights.contains(requestfirstchar))
			return true;
		else
			return false;
	}
}
